package com.qmmessage.sdk.models;

import com.google.gson.reflect.TypeToken;
import com.qmmessage.sdk.common.AbstractModel;
import com.qmmessage.sdk.common.AbstractResult;
import com.qmmessage.sdk.common.exception.QmMessageSdkException;
import com.qmmessage.sdk.common.http.HttpResponse;
import com.qmmessage.sdk.common.profile.ClientProfile;
import com.qmmessage.sdk.common.util.*;

import java.util.Map;

/**
 * @author qm
 */
public class SendMessageModel extends AbstractModel {
    /**
     * message content
     */
    private String content;

    /**
     * message area：China、 Overseas
     */
    private ChannelArea channelArea;
    /**
     * template id
     */
    private Long templateId;
    /**
     * message signature
     */
    private String signature;
    /**
     * single phone number
     */
    private String phone;
    /**
     * param
     * format：code_0, name_1
     */
    private MessageVariableValues paramMap;
    /**
     * ext param value
     */
    private String extParam;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public ChannelArea getChannelArea() {
        return channelArea;
    }

    public void setChannelArea(ChannelArea channelArea) {
        this.channelArea = channelArea;
    }

    public Long getTemplateId() {
        return templateId;
    }

    public void setTemplateId(Long templateId) {
        this.templateId = templateId;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public MessageVariableValues getParamMap() {
        return paramMap;
    }

    public void setParamMap(MessageVariableValues paramMap) {
        this.paramMap = paramMap;
    }

    /**
     * set json format params
     * json format: {"param_0":"val0","param_1":"val1"}
     */
    public void setJsonParam(String json) {
        this.paramMap = QmJsonUtil.fromJson(json, TypeToken.getParameterized(MessageVariableValues.class).getType());
    }

    public String getExtParam() {
        return extParam;
    }

    public void setExtParam(String extParam) {
        this.extParam = extParam;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    @Override
    public <T extends AbstractResult> T internalRequest(ClientProfile profile, Class<T> typeOfT) throws QmMessageSdkException {
        super.initProfile(profile);
        // request params
        String json = QmJsonUtil.toJson(this, SendMessageModel.class);
        Map<String, String> headers = super.initHeaders(profile, json);
        String url = profile.getEndpoint() + "/sdk/sms/send-message";
        HttpResponse httpResponse = QmHttpUtils.doPost(url, json, headers);
        if (httpResponse.getSuccess()) {
            String body = httpResponse.getBody();
            if (QmStrUtils.isNotBlank(body)) {
                return QmJsonUtil.fromJson(body, typeOfT);
            }
        }
        return null;
    }
}
